﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.Collections;
using System.Data;
using System.Configuration;

namespace LDAP
{
    public partial class adbook : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {

            }
        }

        #region LDAP Address Book
        protected void btnGo_Click(object sender, System.EventArgs e)
        {
            if (txtFName.Text == "")
            {
                GridView1.Visible = false;
                DetailsView1.Visible = false;
                abImage.Visible = true;
                return;
            }
            GridView1.Visible = true;
            DetailsView1.Visible = false;
            abImage.Visible = false;
            if (Session["dtDetails"] != null)
            {
                Session.Remove("dtDetails");
            }

            DataTable dtDetails = new DataTable();
            DataRow drDetails = null;
            dtDetails.Columns.Add(new DataColumn("Name", typeof(string)));
            dtDetails.Columns.Add(new DataColumn("Title", typeof(string)));
            dtDetails.Columns.Add(new DataColumn("Email", typeof(string)));
            dtDetails.Columns.Add(new DataColumn("Phone", typeof(string)));
            dtDetails.Columns.Add(new DataColumn("Location", typeof(string)));

            DataTable dtSummary = new DataTable();
            DataRow drSummary = null;
            dtSummary.Columns.Add(new DataColumn("Name", typeof(string)));
            dtSummary.Columns.Add(new DataColumn("Location", typeof(string)));
            
            // Authenticate to Active Directory
            string ldapPath = ConfigurationManager.AppSettings["LDAP"];
            DirectoryEntry entry = new DirectoryEntry(ldapPath);

            try
            {
                object native = entry.NativeObject;
            }
            catch (Exception)
            {
                //return;
                throw;
            }
            try
            {
                //Find all Matches
                DirectorySearcher search = new DirectorySearcher(entry);
                search.Filter = "(&(c=US)(objectClass=organizationalPerson)(displayname= *" + txtFName.Text + "*))";
                search.PropertiesToLoad.Add("displayname");
                search.PropertiesToLoad.Add("title");
                search.PropertiesToLoad.Add("mail");
                search.PropertiesToLoad.Add("telephoneNumber");
                search.PropertiesToLoad.Add("l");

                SearchResultCollection results = search.FindAll();

                if ((results != null))
                {
                    lblResults.Text = "Found " + results.Count;
                    foreach (SearchResult result in results)
                    {
                        drDetails = dtDetails.NewRow();
                        drSummary = dtSummary.NewRow();

                        if (result.Properties.Contains("DisplayName"))
                        {
                            drDetails[0] = result.Properties["DisplayName"][0];
                            drSummary[0] = result.Properties["DisplayName"][0];
                        }

                        if (result.Properties.Contains("title"))
                        {
                            drDetails[1] = result.Properties["title"][0];
                        }
                        else
                        {
                            drDetails[1] = "No Title";
                        }

                        if (result.Properties.Contains("mail"))
                        {
                            drDetails[2] = "<a href='mailto:" + result.Properties["mail"][0] + "'>" + result.Properties["mail"][0] + "</a>";
                        }
                        else
                        {
                            drDetails[2] = "No Email";
                        }

                        if (result.Properties.Contains("telephoneNumber"))
                        {
                            drDetails[3] = result.Properties["telephoneNumber"][0];
                        }
                        else
                        {
                            drDetails[3] = "No Phone";
                        }

                        if (result.Properties.Contains("l"))
                        {
                            drDetails[4] = result.Properties["l"][0];
                            drSummary[1] = result.Properties["l"][0];
                        }
                        else
                        {
                            drDetails[4] = "No Location";
                            drSummary[1] = "No Location";
                        }
                        dtDetails.Rows.Add(drDetails);
                        dtSummary.Rows.Add(drSummary);
                    }
                }
            }
            catch (Exception)
            {
                //return;
                throw;
            }
            
            dtDetails.DefaultView.Sort = "location, name";
            GridView1.DataSource = dtSummary;
            Session.Add("dtDetails", dtDetails);
            GridView1.DataBind();

        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            GridView1.Visible = false;
            DetailsView1.Visible = true;
            if (Session["dtDetails"] != null)
            {
                DataTable dtDetails = (DataTable)Session["dtDetails"];
                Session.Remove("dtDetails");
                dtDetails.DefaultView.RowFilter = "name='" + ((LinkButton)sender).Text + "'";
                DetailsView1.DataSource = dtDetails;
                DetailsView1.DataBind();
                lblResults.Text = "";
            }
            else
            {
                GridView1.Visible = false;
                DetailsView1.Visible = false;
                lblResults.Text = "Session timed out";
            }
        }

        protected void DetailsView1_DataBound(object sender, EventArgs e)
        {
            DetailsView1.Rows[2].Cells[1].Text = Server.HtmlDecode(DetailsView1.Rows[2].Cells[1].Text);
        }
        #endregion

    }
}